<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@include file="common.jsp"%>
<%@page import="java.util.Collection,java.util.Iterator"%>
<jsp:useBean id="cart" scope="session" class="cn.jhc.bean.CartBean" />
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="css/music.css"></link>
<title>吉他小店</title>
</head>
<style>
table {
	background-color: rgba(0, 0, 0, 0.25);
	box-shadow: 0px 0px 3px 1px black;
	margin-bottom:20px;
}
</style>
<body>
	<div id="wrapper">
		<jsp:include page="./header.jsp"></jsp:include>
		<div id="address">
			<a>当前位置： 查看购物车</a>
		</div>
		<div id="box">
			<div class="shengming">
				<h4>声明:</h4>
				<a> 为方便社员购买吉他，本小店提供吉他样式，社员提交订单后，管理员会与你联系，请社员填写真实信息!</a>
			</div>
		</div>
		<div id="shopcart" align="center">
			<%
				request.setCharacterEncoding("UTF-8");
				String action = request.getParameter("action");
				if (action != null && action.equals("保存修改")) {
					String strItemNum = request.getParameter("itemnum");
					if (null == strItemNum || "".equals(strItemNum)) {
						throw new ServletException("非法的参数");
					}
					int itemNum = Integer.parseInt(strItemNum);
					for (int i = 0; i < itemNum; i++) {
						String strNum = request.getParameter("num_" + i);
						String strIsBn = request.getParameter("isbn_" + i);

						int quantity = Integer.parseInt(strNum);
						int isbn = Integer.parseInt(strIsBn);

						boolean bEnough = shopdb.isAmountEnough(isbn, quantity);
						if (bEnough) {
							cart.setItemNum(new Integer(isbn), quantity);
						} else {
							ShopBean shop = shopdb.getShop(strIsBn);
							out.println("<font color=\"red\" size=\"4\">");
							out.println(shop.getTitle());
							out.println("的库存只有" + shop.getAmount()
									+ "件，请调整购买数量！<p>");
							out.println("</font>");

						}

					}
				}
			%>
			<%
				Collection<CartItemBean> cl = cart.getItems();
				if (cl.size() <= 0) {
					out.println("<font color=\"orange\" size=\"4\">");
					out.println("购物车中没有商品<p>");
					out.println("</font>");
			%>
			<div id="text_link">
				<a href="showallgoods.do?cond=all">继续购物</a>
			</div>
		</div>
		<div id="button_bg"></div>
		<div id="line2"></div>
		<div id="copyright">
			<a>&copy;版权所有:金职院蓝调吉他社 </a><a>| </a>
			<div id="admin">
				<a href="houtai_index.jsp">管理员登录</a>
			</div>
		</div>
		<div>
			<%
				return;
				}
				Iterator<CartItemBean> it = cl.iterator();
			%>
			<form name="theform" action="showcart.jsp" method="post">
				<table width="890">
					<tr height="30">
						<th>名称</th>
						<th>价格</th>
						<th>数量</th>
						<th>小计</th>
						<th>操作</th>
					</tr>
					<%
						int i = 0;
						while (it.hasNext()) {
							CartItemBean cartItem = (CartItemBean) it.next();
							ShopBean shop = cartItem.getShop();
							String isbn = shop.getIsbn();
							String fieldNum = "num_" + i;
							String fieldshop = "isbn_" + i;
					%>
					<tr height="30">
						<td><div align="center"><%=shop.getTitle()%></div></td>
						<td><div align="center"><%=shop.getPrice()%></div></td>
						<td><div align="center">
								<input type="text" name="<%=fieldNum%>"
									value="<%=cartItem.getQuantity()%>" aize="2" /> <input
									type="hidden" name="<%=fieldshop%>" value="<%=isbn%>" />
							</div></td>
						<td><div align="center"><%=cartItem.getItemPrice()%></div></td>
						<td><div id="text_link1" align="center">
								<a href="delitem.jsp?id=<%=isbn%>">删除</a>
							</div></td>

					</tr>
					<%
						i++;
						}
					%>
					<tr height="30">
						<td><div align="center">合计</div></td>
						<td colspan="4">&nbsp;&nbsp;&nbsp;<%=cart.getTotalPrice()%></td>
					</tr>
				</table>
				<input type="hidden" name="itemnum" value="<%=i%>" /> <input
					type="submit" name="action" value="保存修改" />
				&nbsp;&nbsp;&nbsp;&nbsp;
				<div id="text_link">
					<a href="showallgoods.do?cond=all">继续购物</a>
					&nbsp;&nbsp;&nbsp;&nbsp; <a
						href="settlement.jsp?getb=/settlement.jsp">提交订单</a>
				</div>
			</form>
		</div>
		<div id="button_bg"></div>
		<div id="line2"></div>
		<div id="copyright">
			<a>&copy;版权所有:蓝调吉他社 </a><a>| </a>
			<div id="admin">
				<a href="houtai_index.jsp">管理员登录</a>
			</div>
		</div>
	</div>
</body>
</html>